#include "widget.h"
#include "ui_widget.h"
#include "Biquad.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

int32_t float_to_q30_hex(double val) {
    int64_t q30 = (int64_t)round(val * (1LL << 30));
    if (q30 < 0) {
        uint32_t hex = (uint32_t)(q30 & 0xFFFFFFFF);
        // //printf("-0x%x\n", (uint32_t)(-q30 & 0xFFFFFFFF));
        return (int32_t)hex;
    } else {
        return (int32_t)q30;
    }
}

void Widget::on_ButtonLowpass_clicked()
{
    if (ui->editFc->text().isEmpty() ||
        ui->editGain->text().isEmpty() ||
        ui->editSampleRate->text().isEmpty() ||
        ui->editQ->text().isEmpty()) {
        return;
    }
    

    double SampleRate = ui->editSampleRate->text().toDouble();
    double Fc = ui->editFc->text().toDouble();
    double Q  = ui->editQ->text().toDouble();
    double peakGainDB = ui->editGain->text().toDouble();

    Biquad eqBiquad;
    eqBiquad.setBiquad(bq_type_lowpass, Fc/SampleRate, Q, peakGainDB);
    eqBiquad.float_to_q30_hex();

    // 18
    this->bugData += QString("\n---- lowpass ----\n");
    this->bugData += QString("SampleRate:%1 , Fc:%2, Q:%3, Gain:%4\n").arg(SampleRate, 0, 'f', 0)
                                                                   .arg(Fc, 0, 'f', 0)
                                                                   .arg(Q, 0, 'f', 2)
                                                                   .arg(peakGainDB, 0, 'f', 2);
    this->bugData += QString("\na0: %1,\na1: %2,\na2: %3,\nb1: %4,\nb2: %5\r\n")
        .arg(eqBiquad.a0, 0, 'f', 18)
        .arg(eqBiquad.a1, 0, 'f', 18)
        .arg(eqBiquad.a2, 0, 'f', 18)
        .arg(eqBiquad.b1, 0, 'f', 18)
        .arg(eqBiquad.b2, 0, 'f', 18);

    this->bugData += QString("\na0: 0x%1,\na1: 0x%2,\na2: 0x%3,\nb1: 0x%4,\nb2: 0x%5\r\n")
                         .arg(eqBiquad.buff[0], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[1], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[2], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[3], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[4], 8, 16, QChar('0'));

    ui->textEdit->setText( this->bugData);
}



void Widget::on_ButtonPeak_clicked()
{
    if (ui->editFc->text().isEmpty() ||
        ui->editGain->text().isEmpty() ||
        ui->editSampleRate->text().isEmpty() ||
        ui->editQ->text().isEmpty()) {
        return;
    }


    double SampleRate = ui->editSampleRate->text().toDouble();
    double Fc = ui->editFc->text().toDouble();
    double Q  = ui->editQ->text().toDouble();
    double peakGainDB = ui->editGain->text().toDouble();


    Biquad eqBiquad;
    eqBiquad.setBiquad(bq_type_peak, Fc/SampleRate, Q, peakGainDB);
    eqBiquad.float_to_q30_hex();

    // 18
    this->bugData += QString("\n---- peak ----\n");
    this->bugData += QString("SampleRate:%1 , Fc:%2, Q:%3, Gain:%4\n").arg(SampleRate, 0, 'f', 0)
                         .arg(Fc, 0, 'f', 0)
                         .arg(Q, 0, 'f', 2)
                         .arg(peakGainDB, 0, 'f', 2);
    this->bugData += QString("\na0: %1,\na1: %2,\na2: %3,\nb1: %4,\nb2: %5\r\n")
                         .arg(eqBiquad.a0, 0, 'f', 18)
                         .arg(eqBiquad.a1, 0, 'f', 18)
                         .arg(eqBiquad.a2, 0, 'f', 18)
                         .arg(eqBiquad.b1, 0, 'f', 18)
                         .arg(eqBiquad.b2, 0, 'f', 18);

    this->bugData += QString("\na0: 0x%1,\na1: 0x%2,\na2: 0x%3,\nb1: 0x%4,\nb2: 0x%5\r\n")
                         .arg(eqBiquad.buff[0], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[1], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[2], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[3], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[4], 8, 16, QChar('0'));

    ui->textEdit->setText( this->bugData);
}

void Widget::on_ButtonHighpass_clicked()
{
    if (ui->editFc->text().isEmpty() ||
        ui->editGain->text().isEmpty() ||
        ui->editSampleRate->text().isEmpty() ||
        ui->editQ->text().isEmpty()) {
        return;
    }


    double SampleRate = ui->editSampleRate->text().toDouble();
    double Fc = ui->editFc->text().toDouble();
    double Q  = ui->editQ->text().toDouble();
    double peakGainDB = ui->editGain->text().toDouble();


    Biquad eqBiquad;
    eqBiquad.setBiquad(bq_type_highpass, Fc/SampleRate, Q, peakGainDB);
    eqBiquad.float_to_q30_hex();

    // 18
    this->bugData += QString("\n---- highpass ----\n");
    this->bugData += QString("SampleRate:%1 , Fc:%2, Q:%3, Gain:%4\n").arg(SampleRate, 0, 'f', 0)
                         .arg(Fc, 0, 'f', 0)
                         .arg(Q, 0, 'f', 2)
                         .arg(peakGainDB, 0, 'f', 2);
    this->bugData += QString("\na0: %1,\na1: %2,\na2: %3,\nb1: %4,\nb2: %5\r\n")
                         .arg(eqBiquad.a0, 0, 'f', 18)
                         .arg(eqBiquad.a1, 0, 'f', 18)
                         .arg(eqBiquad.a2, 0, 'f', 18)
                         .arg(eqBiquad.b1, 0, 'f', 18)
                         .arg(eqBiquad.b2, 0, 'f', 18);

    this->bugData += QString("\na0: 0x%1,\na1: 0x%2,\na2: 0x%3,\nb1: 0x%4,\nb2: 0x%5\r\n")
                         .arg(eqBiquad.buff[0], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[1], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[2], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[3], 8, 16, QChar('0'))
                         .arg(eqBiquad.buff[4], 8, 16, QChar('0'));


    ui->textEdit->setText( this->bugData);
}



void Widget::on_ButtonClear_clicked()
{
    this->bugData.clear();
    ui->textEdit->clear();
}


void Widget::on_ButtonPeak_2_clicked()
{
    Biquad eqBiquad;
    eqBiquad.a0 = this->ui->editA0->text().toDouble();
    eqBiquad.a1 = this->ui->editA1->text().toDouble();
    eqBiquad.a2 = this->ui->editA2->text().toDouble();
    eqBiquad.b1 = this->ui->editB1->text().toDouble();
    eqBiquad.b2 = this->ui->editB2->text().toDouble();

    eqBiquad.float_to_q30_hex();

    this->bugData += QString("\n---- 转换数值 ----\n");
    this->bugData += QString("\na0: %1,\na1: %2,\na2: %3,\nb1: %4,\nb2: %5\r\n")
                         .arg(eqBiquad.a0, 0, 'f', 18)
                         .arg(eqBiquad.a1, 0, 'f', 18)
                         .arg(eqBiquad.a2, 0, 'f', 18)
                         .arg(eqBiquad.b1, 0, 'f', 18)
                         .arg(eqBiquad.b2, 0, 'f', 18);

     this->bugData += QString("\na0: 0x%1,\na1: 0x%2,\na2: 0x%3,\nb1: 0x%4,\nb2: 0x%5\r\n")
                          .arg(eqBiquad.buff[0], 8, 16, QChar('0'))
                          .arg(eqBiquad.buff[1], 8, 16, QChar('0'))
                          .arg(eqBiquad.buff[2], 8, 16, QChar('0'))
                          .arg(eqBiquad.buff[3], 8, 16, QChar('0'))
                          .arg(eqBiquad.buff[4], 8, 16, QChar('0'));

     ui->textEdit->setText( this->bugData);
}

